package com.carlinfo.hospital.dao.impl;

import com.carlinfo.hospital.dao.OrderDao;
import com.carlinfo.hospital.pojo.Order;
import com.carlinfo.hospital.pojo.Person;
import com.carlinfo.hospital.util.DataSourceUtil;
import com.carlinfo.hospital.util.PageInfoUtil;
import com.carlinfo.hospital.util.ReflectUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.omg.DynamicAny._DynAnyFactoryStub;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;

@Repository
public class OrderDaoImpl implements OrderDao {
    private DataSourceUtil dataSourceUtil;
    DataSource dataSource = dataSourceUtil.getInstance();
    QueryRunner queryRunner = new QueryRunner(dataSource);
    String sql = "";
//id doctorId content  money  status createTime  updateTime publicTime

    @Override
    public int saveOneDao(Order order) {
        sql = "insert into userorder values(null,?,?,?,?,?,?,?)";
        DataSource dataSource = dataSourceUtil.getInstance();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        try {
            Object[] zh = ReflectUtil.zh(order);
            System.out.println(Arrays.toString(zh));
            int execute = queryRunner.execute(sql, order.getPersonId(),order.getContent(),order.getMoney(),order.getStatus(),order.getCreateTime()
            ,order.getUpdateTime(),new Date());
            System.out.println(execute);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Order> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        sql = "select * from order where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
            sql += " and content like ? ";
            list.add("%" + condMap.get("keyword") + "%");
        }
        if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + "")) {
            sql += " and status like ? ";
            list.add("%" + condMap.get("status") + "%");
        }
        if (condMap.get("stDate") != null && !"".equalsIgnoreCase(condMap.get("edDate") + "")) {
            sql += " and (publicTime>? and publicTime<?)";
            list.add(condMap.get("stDate"));
            list.add(condMap);
        }
        System.out.println("sql:" + sql);
        String[] s = new String[list.size()];
        String[] strings = list.toArray(s);
        try {
            List<Order> res = queryRunner.query(sql, new BeanListHandler<Order>(Order.class), strings);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Order selectOneDao(Map<String, Object> condMap) {
        List<Object> list = new ArrayList<Object>();
        if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null) {
            sql = "select * from order where money= ? ";
            list.add(condMap.get("name"));
        }
        if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null) {
            sql = "select * from order where id= ? ";
            list.add(condMap.get("id"));
        }
        try {
            Order res = queryRunner.query(sql, new BeanHandler<Order>(Order.class), list.get(0));
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateOneDao(Order order) {
        //id doctorId content  money  status createTime  updateTime publicTime
        sql = "update order set doctorId=?,content=?,money=?,status=?,createTime=?,updateTime=? ,publicTime=?,publicTime=? where id =?";
        try {
            int execute = queryRunner.execute(sql, ReflectUtil.zh(order));
            return execute;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteOneDao(Map<String, Object> condMap) {
        List<Object> list = new ArrayList<Object>();
        if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null) {
            sql = "delete * from order where money= ? ";
            list.add(condMap.get("name"));
        }
        if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null) {
            sql = "delete * from order where id= ? ";
            list.add(condMap.get("id"));
        }
        String[] strings = new String[list.size()];
        try {
            int execute = queryRunner.execute(sql, list.toArray(strings));
            return execute;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
